<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ taglib prefix="ujn" uri="http://ujn.edu.cn/common/"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName()
            + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE HTML>
<html>

<head>
    <title>demo.jsp</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- 引入css样式文件 -->
    <!-- Bootstrap Core CSS -->
    <link href="<%=basePath%>css/bootstrap.css" rel="stylesheet" />
    <!-- Bootstrap-grid CSS -->
    <link href="<%=basePath%>css/bootstrap-grid.css" rel="stylesheet" />
    <!-- Bootstrap-reboot CSS -->
    <link href="<%=basePath%>css/bootstrap-reboot.css" rel="stylesheet" />
</head>
<body>

<div>
    <nav class="navbar navbar-expand-lg navbar-dark " style="background-color:#81D8CF;;">
        <img src="imges/logo.jpg" class="rounded float-left" alt="Logo" height="45" width="45">
        <div class="container" style="">
            <a class="navbar-brand" href="#">停车场管理系统</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText"
                    aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarText">
                <ul class="navbar-nav mr-auto">
                    <li class="nav-item">
                        <a class="nav-link " href="${pageContext.request.contextPath }/userself">个人信息</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link " href="${pageContext.request.contextPath }/bookself">车位预定</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="${pageContext.request.contextPath}/payments">全部订单</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="${pageContext.request.contextPath}/coupon">优惠券</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="${pageContext.request.contextPath }/rateself">收费标准</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="${pageContext.request.contextPath }/vehicleself">车辆信息</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="${pageContext.request.contextPath }/userequip">投诉反馈</a>
                    </li>
                </ul>
                <span class="navbar-text">
          导航栏
        </span>
            </div>
        </div>
    </nav>
</div>


<%

    if (session.getAttribute("USER_SESSION")!=null) {
%>

<!--登录成功界面-->
<div class="container" style="padding-top: 90px; text-align: center; background-color: rgba(255, 255, 255, 0.7);">
    <div class="jumbotron">
        <h1 class="display-4">您需支付<span id="finalCost">${cost}</span> 元</h1>
        <div class="container">
            <p class="lead">请在下方选择优惠券，使用微信或支付宝付款。</p>
        </div>
    </div>
    <div class="form-group d-flex justify-content-center align-items-center" style="margin-top: 20px;">
        <label for="couponSelect" style="margin-right: 10px;">选择优惠券：</label>
        <select class="form-control" id="couponSelect" onchange="updateTotalCost()" style="width: auto; display: inline-block; margin-right: 10px;">
            <option value="0" data-id="0" selected>不使用优惠券</option>
            <c:forEach items="${coupons}" var="coupon">
                <option value="${coupon.value}" data-id="${coupon.couponId}">${coupon.value} 元</option>
            </c:forEach>
        </select>
        <button type="button" class="btn btn-primary" onclick="showPaymentModal()">确认支付</button>
    </div>
</div>

<!-- 模态框 -->
<div class="modal fade" id="paymentModal" tabindex="-1" role="dialog" aria-labelledby="paymentModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <input type="hidden" id="originalCost" value="${cost}">
        <input type="hidden" id="hiddenFinalCost" value="${cost}">
        <input type="hidden" id="hiddenCouponValue" value="0">
        <input type="hidden" id="hiddenCouponId" value="0">
        <input type="hidden" name="payment_id" value="<%= request.getParameter("payment_id") %>">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="paymentModalLabel">支付确认</h5>
                <!-- Bootstrap 4 的关闭按钮使用 `data-dismiss` -->
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p>请扫码支付：</p>
                <img src="imges/gitee.jpg" alt="gitee仓库" style="width: 50%; margin: 5px;">
            </div>
            <div class="modal-footer">
                <!-- 取消按钮 -->
                <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                <!-- 确认支付按钮 -->
                <button type="button" class="btn btn-primary" onclick="confirmPayment()">确认已支付</button>
            </div>
        </div>
    </div>
</div>

<%
} else{
%>
<!--登陆失败界面-->
<div class="jumbotron jumbotron-fluid">
    <div class="container">
        <h1 class="display-4">欢迎您,登录后即可查看</h1>
        <p class="lead">
            <a href="${pageContext.request.contextPath}/login">Welcome, log in to view</a>
        </p>
    </div>
</div>
<%  }
%>


<!-- 引入js文件 -->
<script src="<%=basePath%>js/jquery-1.11.3.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="<%=basePath%>js/bootstrap.js"></script>
<!-- Bootstrap-bundle JavaScript -->
<script src="<%=basePath%>js/bootstrap.bundle.js"></script>
<!-- Metis Menu Plugin JavaScript -->
<script src="<%=basePath%>js/metisMenu.min.js"></script>
<!-- DataTables JavaScript -->
<script src="<%=basePath%>js/jquery.dataTables.min.js"></script>
<script src="<%=basePath%>js/dataTables.bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="<%=basePath%>js/sb-admin-2.js"></script>

<script>
    function updateTotalCost() {
        // 获取原始金额
        const originalCost = parseFloat(document.getElementById("originalCost").value);
        // 获取选中的优惠券的值和ID
        const couponSelect = document.getElementById("couponSelect");
        const couponValue = parseFloat(couponSelect.value || 0); // 优惠金额
        const couponId = couponSelect.options[couponSelect.selectedIndex].getAttribute("data-id"); // 优惠券ID
        // 计算最终金额
        const finalCost = originalCost - couponValue;
        // 更新页面中显示的最终金额
        document.getElementById("finalCost").textContent = finalCost.toFixed(2);
        // 同步到隐藏字段
        document.getElementById("hiddenFinalCost").value = finalCost.toFixed(2);
        document.getElementById("hiddenCouponValue").value = couponValue.toFixed(2);
        document.getElementById("hiddenCouponId").value = couponId;
    }
    function showPaymentModal() {
        $('#paymentModal').modal('show');
    }
    function confirmPayment() {
        const paymentId = document.querySelector('input[name="payment_id"]').value;
        const finalCost = document.getElementById("hiddenFinalCost").value;
        const couponId = document.getElementById("hiddenCouponId").value;

        if (confirm('确定已完成支付吗？')) {
            $.ajax({
                url: `<%=basePath%>completePayment`,
                type: 'POST',
                data: {
                    paymentId: paymentId,
                    finalCost: finalCost,
                    couponId: couponId
                },
                success: function (data) {
                    if (data === "success") {
                        alert("支付成功！");
                        window.location.href = '<%=basePath%>payments'; // 支付成功后跳转到订单页面
                    } else {
                        alert("支付失败，请重试！");
                    }
                },
                error: function (xhr, status, error) {
                    console.error("支付请求失败：", error);
                    alert("支付失败，请稍后再试！");
                }
            });
        }
    }

</script>
</body>
</html>
